package prototypePatter1;

import cn.hutool.core.util.ObjectUtil;
import lombok.ToString;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 582895699@qq.com
 * @Desctription: 深拷贝
 * @Date: Created in 2021/12/17 11:05
 */
@ToString
public class DeepClone implements Serializable {
    private static final long serialVersionUID = -174641130567660496L;
    private int age;
    private List<String> list = new ArrayList<>();

    public DeepClone() {
    }

    public DeepClone(int age, List<String> list) {
        this.age = age;
        this.list = list;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public List<String> getList() {
        return list;
    }

    public void setList(String str) {
        this.list.add(str);
    }

    public static void main(String[] args) throws CloneNotSupportedException {
        // 初始化原对象
        DeepClone deepClone = new DeepClone();
        deepClone.setAge(12);
        deepClone.setList("123");

        // 执行深拷贝
        DeepClone clone = ObjectUtil.cloneByStream(deepClone);
        clone.setAge(11);
        clone.setList("456");
        clone.setList("456");
        clone.setList("456");

        // DeepClone(age=12, list=[123])
        System.out.println(deepClone.toString());
        // DeepClone(age=11, list=[123, 456, 456, 456])   对clone的改变并没有影响deepClone
        System.out.println(clone.toString());
    }
}
